<?php

$db = new Berita();
$table = "berita";
$berita = $db->lihat($table,"","idberita");

$o .= '<a href="?page=addberita"><i class="icon-plus"></i> Tambah Data</a>';

if ($berita) {
	
	if (isset($_GET['success']) && ($_GET['success'] == "1")) {

	$o .= '<div class="alert alert-success">
			hapus data berita berhasil
			</div>';
				
	} elseif (isset($_GET['success']) && ($_GET['success'] == "0")) {
			
		$o .= '<div class="alert alert-error">
				hapus data berita gagal
			   </div>';
	}

	$o .= '<table class="table table-striped table-bordered table-condensed">
			<tr>
				<th><center>No</th>
				<th><center>Judul</th>
				<th><center>Tanggal</th>
				<th><center>Penulis</th>
				<th colspan="3"><center>Tindakan</th>
			</tr>
	
		  ';
	
	$no = 1;
	
	foreach($berita as $x) {
		
		$o .= '<tr>
					<td><center>'.$no.'</td>
					<td><center>'.$x['judul'].'</td>
					<td><center>'.$x['tgl'].'</td>
					<td><center>'.$x['nama'].'</td>
					<td><center><a data-toggle="modal" href="#myModal'.$no.'"><i class="icon-zoom-in"></i> Detail</a></td>
					<td><center><a href="?page=editberita&idberita='.$x['idberita'].'"><i class="icon-wrench"></i> Ubah</a></td>
					<td><center><a href="?page=berita&action=remove&idberita='.$x['idberita'].'"><i class="icon-remove"></i> Hapus</a></td>
			  </tr>';

			  $o .= ' <div id="myModal'.$no.'" class="modal hide fade">
			           <div class="modal-header">
			              <button class="close" data-dismiss="modal">&times;</button>
			           		<center><img src="../file/berita/'.$x['gambar'].'"></center>
			           		<br>
			             	'.html_entity_decode($x['isi']).'
			            </div>
			          </div>';
			  
		$no++;
		
	}

	$o .= '</table>';

} else {

	$o .= '<div class="alert alert-error">
		  <center>Data kosong</center>
		  </div>';
} 

echo $o;

if ($_GET['action'] == "remove") {

	$id = htmlentities(mysql_real_escape_string($_GET['idberita']));
	
	$hapusberita = $db->hapus($table,$id);
	
	if ($hapusberita) {
		?>
			<script language="javascript">
			document.location="?page=berita&success=1";
			</script>
		<?php
		
	} else {
		?>
			<script language="javascript">
			document.location="?page=berita&success=0";
			</script>
		<?php
	}
}